﻿using System;
using System.Text;
using System.IO;

namespace WebTimer.ScheduledEvents
{
    public sealed class EventLogs
    {
        public string LogFileName = string.Empty;

        public void WriteFailedLog(string logContent)
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(DateTime.Now);
                builder.Append("\t");
                builder.Append(Environment.MachineName);
                builder.Append("\t");
                builder.Append(logContent);
                builder.Append("\r\n");
                using (FileStream fs = new FileStream(LogFileName, FileMode.Append, FileAccess.Write, FileShare.Write))
                {
                    Byte[] info = System.Text.Encoding.UTF8.GetBytes(builder.ToString());
                    fs.Write(info, 0, info.Length);
                    fs.Close();
                }
            }
            catch
            {
                WriteEmptyLog();
            }
        }

        public void WriteEmptyLog()
        {
            using (FileStream fs = new FileStream(LogFileName, FileMode.Truncate, FileAccess.Write, FileShare.Write))
            {
                Byte[] info = System.Text.Encoding.UTF8.GetBytes(string.Empty);
                fs.Write(info, 0, info.Length);
                fs.Close();
            }
        }
    }
}
